/**
 *
 * @Author 董熠涛
 * @Date 2025/6/16 20:22
 */
package example.springboot.service;

import example.springboot.dto.PageResult;
import example.springboot.entity.SystemLog;
import example.springboot.mapper.SystemLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemLogService {

    @Autowired
    private SystemLogMapper systemLogMapper;

    /**
     * 分页查询系统日志
     */
    public PageResult<SystemLog> getSystemLogList(String type, String date, int page, int size) {
        int offset = (page - 1) * size;
        List<SystemLog> logs = systemLogMapper.findByPage(type, date, offset, size);
        int total = systemLogMapper.countLogs(type, date);

        return new PageResult<>(logs, (long) total);
    }

    /**
     * 根据ID查询系统日志
     */
    public SystemLog findById(Integer id) {
        return systemLogMapper.findById(id);
    }

    /**
     * 创建系统日志
     */
    public SystemLog createSystemLog(SystemLog log) {
        systemLogMapper.insert(log);
        return log;
    }

    /**
     * 删除系统日志
     */
    public boolean deleteSystemLog(Integer id) {
        return systemLogMapper.deleteById(id) > 0;
    }

    /**
     * 清空所有日志
     */
    public boolean clearAllLogs() {
        return systemLogMapper.deleteAll() > 0;
    }
}

